package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class Gy3yNewsAnalyse implements AnalyseNews {

	//http://www.gy3y.com/cn/news_info.aspx?id=5162&cid=71&mode=mtjj
	//http://www.gy3y.com/cn/BYKSnews_info.aspx?id=5073&cid=262
	private static final String[] regex={
		"http://www.gy3y.com/[a-z]*/[a-z]*/[a-z]*_[0-9]*.html",
		"http://www.gy3y.com/pic/css/[a-zA-Z]*.css",
		"http://www.gy3y.com/pic/css/[a-z]*.css"};
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}
	
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		
		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title=doc.select("h2").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("html body form#form2 div.bigdiv div.center div.right_ div.right_about div.h1list_about ul table tbody tr td table tbody tr td div:eq(0)").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.rightCon"));
		date=DateUtils.matchDate(doc.select("span.sjsj_l").text());
		if(StringUtils.isBlank(title)){
		}
		news.setUrl(url);		
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		return news;
	}
	
	public static void main(String[] args) {
		Gy3yNewsAnalyse test=new Gy3yNewsAnalyse();
		String url="http://www.gy3y.com/xwzx/yyxw/a_103191.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
